package com.wjh.web.api;

import com.alibaba.fastjson2.JSON;
import com.wjh.base.ResponseApiData;
import com.wjh.service.IUserService;
import com.wjh.user.request.UpdateAmountRequest;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户对外 API接口
 * 
 * @author wenjianhai
 * @date 2023/6/29
 * @since JDK 17
 */
@Tag(name = "用户对外 AIP接口", description = "用户对外 AIP接口")
@Slf4j
@RestController
@RequestMapping("/user/api")
public class UserApiController {

    @Autowired
    private IUserService userService;

    @Operation(summary = "更新用户可用金额", description = "更新用户可用金额")
    @PostMapping("/update/amount")
    public ResponseApiData<?> updateAmount(@Validated @RequestBody UpdateAmountRequest request) {
        log.info(request.getTip() + ",更新用户可用金额-开始.请求参数:{}", JSON.toJSONString(request));
        try {
            userService.updateAmount(request);
            log.info(request.getTip() + ",更新用户可用金额-结束.请求参数:{}", JSON.toJSONString(request));
            return ResponseApiData.success();
        } catch (Exception e) {
            log.error(String.format(request.getTip() + ",更新用户可用金额-失败.请求参数:%s", JSON.toJSONString(request)), e);
            return ResponseApiData.systemFail("更新用户可用金额失败:" + e.getMessage());
        }
    }
}
